package com.example.meituan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.meituan.R;
import com.example.meituan.bean.OrderBean;
import com.example.meituan.bean.OrderFoodBean;
import com.squareup.picasso.Picasso;

import java.util.List;

public class CustomAdapter extends BaseAdapter {
    private Context context;
    private List<OrderBean> orderList;

    public CustomAdapter(Context context, List<OrderBean> orderList) {
        this.context = context;
        this.orderList = orderList;
    }

    @Override
    public int getCount() {
        return orderList.size();
    }

    @Override
    public Object getItem(int position) {
        return orderList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item, null);
        }
        TextView shopNameTextView = convertView.findViewById(R.id.shopNameTextView);
        TextView order_message = convertView.findViewById(R.id.order_message);
        TextView order_time = convertView.findViewById(R.id.order_time);
        TextView foodNameTextView = convertView.findViewById(R.id.foodNameTextView);
//        TextView quantityTextView = convertView.findViewById(R.id.quantityTextView);
        TextView totalPriceTextView = convertView.findViewById(R.id.totalPriceTextView);
        ImageView foodPicImageView = convertView.findViewById(R.id.foodPicImageView);
        ImageView shopPicImageView = convertView.findViewById(R.id.iv_shop_image);
        OrderBean order = orderList.get(position);

        // Set shop name to the shopNameTextView
        shopNameTextView.setText("商家："+order.getShopName());
        order_message.setText("备注："+order.getMessage());
        order_time.setText("时间："+order.getTime());
        // Use Picasso or Glide to load shop pic into the shopPicImageView
        Picasso.get().load(order.getShopPic()).into(shopPicImageView);

        // Display the first food item's details (for demonstration)
        if (order.getOrderItems() != null && order.getOrderItems().size() > 0) {
            OrderFoodBean firstFood = order.getOrderItems().get(0);
            foodNameTextView.setText("商品："+firstFood.getFoodName());
//            quantityTextView.setText("数量"+String.valueOf(firstFood.getQuantity())); // Convert quantity to String
            totalPriceTextView.setText("实付金额" + String.valueOf(firstFood.getTotalPrice())+"￥"); // Convert totalPrice to String and prepend with '$'
           Picasso.get().load(order.getOrderItems().get(0).getFoodPic()).into(foodPicImageView);
            // Use Picasso or Glide to load food pic into the foodPicImageView

        }


        return convertView;
    }
}


